import Vue from 'vue'
import Vuex from 'vuex'
// import jpushIM from '@/common/js/im.js'
// import imUtils from '@/common/js/imTools.js'

Vue.use(Vuex)

const store = new Vuex.Store({
	state: {
		userInfo: {},
		couponList:[],
		stepData: [],
		targetSteps:uni.getStorageSync('mbSteps'), // 目标 步数
		watchInfor:{
			deviceName:  uni.getStorageSync("deviceName"),
			mac: uni.getStorageSync("mac"),
		},
		imgUrl: 'https://gtbbs.oss-cn-beijing.aliyuncs.com/app/',
		userInfo: uni.getStorageSync("userInfo"),
		hasLogin: uni.getStorageSync("hasLogin")||false,
		sessionKey: uni.getStorageSync("sessionKey"),
		messegeNum:0,
		user_fd:"",  //用户websocket设备id
		homeInfor0: [],
		homeInfor1: [],
		homeInfor2: [],
		homeInfor4: [],
		homeInfor5: [],
		homeInfor6: [],
		seconds:0,
	}, 
	getters:{
		getCouponList(state){
		  return state.couponList
		}
    },
	mutations: {
		//运动数据
		changeStepData(state,infor){
			state.stepData  = infor
		},
		//运动时间
		changeSecond(state,infor){
			state.seconds = infor
		},
		//手表蓝牙链接
		linkBluetooth(state,infor){
			console.log('蓝牙链接')
			state.watchInfor.deviceName = uni.getStorageSync("deviceName");
			state.watchInfor.mac = uni.getStorageSync("mac");

			// uni.setStorageSync("deviceRssi", res.device.deviceRssi);
		},
		//断开链接
		offBluetooth(state){
			console.log('断开链接')
			uni.setStorageSync("deviceName", null);
			uni.setStorageSync("mac", null);
			uni.setStorageSync("deviceRssi", null);
		},
		changeDB(state,infor){
			// console.log(state,infor)
			state.homeInfor1 = infor;
			
		},
		changeStep(state,infor){
			// console.log(state,infor)
			state.homeInfor0 = infor;
			
		},
		
		changeHeart(state,infor){
			// console.log(state,infor)
			state.homeInfor2 = infor;
			
		},
		changeOOValue(state,infor){
			// console.log(infor,'changeOOvalue')
			state.homeInfor4 = infor;
			
		},
		changeTempIntValue(state,infor){
			console.log(infor,'changeTempIntValue')
			state.homeInfor6 = infor;
		},
		changeRespiratoryRateValue(state,infor){
			console.log(infor,'changeRespiratoryRateValue')
			state.homeInfor5 = infor;
		},
		setCouponList(state,arr){
		  state.couponList = arr
		},
		clearCouponList(state){
			console.log('清空所有的券');
			 state.couponList=[]
		},
		login(state) {
			console.log('登录成功')
			uni.setStorageSync('hasLogin', true);
			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					state.hasLogin = true;
					state.userInfo = res.data;
				}
			});
		
		},
		logout(state) {
			let pages = getCurrentPages();
			let curPage = pages[pages.length - 1];
			if (curPage.route != 'pages/account/login') {
				state.hasLogin = false;
				state.userInfo = {};
				uni.removeStorageSync('token');
				uni.removeStorageSync('userInfo');
				uni.redirectTo({
					url: '/pages/login/login'
				})
			}
		},
		update_userinfo(state, info) {
			uni.setStorage({
				key: 'userInfo',
				data: info
			});
			state.userInfo = info;
		},
	},
	actions: {}
})

export default store
